#include "widgetboxcategorymodel.h"

WidgetBoxCategoryEntry::WidgetBoxCategoryEntry() :
    editable(false)
{
}

WidgetBoxCategoryEntry::WidgetBoxCategoryEntry(const Widget &w,
                                               const QIcon &i, bool e) :
    widget(w),
    icon(i),
    editable(e)
{
}



WidgetBoxCategoryModel::WidgetBoxCategoryModel(QObject *parent):
    QAbstractListModel(parent),
    m_viewMode(QListView::ListMode)
{
}

QListView::ViewMode WidgetBoxCategoryModel::viewMode() const
{
    return m_viewMode;
}

void WidgetBoxCategoryModel::setViewMode(QListView::ViewMode vm)
{
    if (m_viewMode == vm)
        return;
    m_viewMode = vm;
    if (!m_items.empty())
        reset();
}

int WidgetBoxCategoryModel::indexOfWidget(const QString &name)
{
    const int count = m_items.size();
    Widget wid;
    for (int  i = 0; i < count; i++)
    {
        wid=m_items.at(i).widget;
        if (wid.name()==name)
            return i;
    }
    return -1;
}

Category WidgetBoxCategoryModel::category() const
{
    Category rc;
    const WidgetBoxCategoryEntrys::const_iterator cend = m_items.constEnd();
    for (WidgetBoxCategoryEntrys::const_iterator it = m_items.constBegin(); it != cend; ++it)
        rc.addWidget(it->widget);
    return rc;
}

void WidgetBoxCategoryModel::addWidget(const Widget &widget, const QIcon &icon,bool editable)
{
    WidgetBoxCategoryEntry item(widget,icon, editable);
    const int row = m_items.size();
    beginInsertRows(QModelIndex(), row, row);
    m_items.push_back(item);
    endInsertRows();
}

QVariant WidgetBoxCategoryModel::data(const QModelIndex &index, int role) const
{
    const int row = index.row();
    if (row < 0 || row >=  m_items.size())
        return QVariant();

    WidgetBoxCategoryEntry item = m_items.at(row);
    switch (role) {
    case Qt::DisplayRole:
        return QVariant(m_viewMode == QListView::ListMode ? item.widget.name() : QString());
    case Qt::DecorationRole:
        return QVariant(item.icon);
    case Qt::EditRole:
        return QVariant(item.widget.name());
    case Qt::ToolTipRole:
        return QVariant(item.widget.name());
    }
    return QVariant();
}

Qt::ItemFlags WidgetBoxCategoryModel::flags(const QModelIndex &index) const
{
    Qt::ItemFlags rc = Qt::ItemIsEnabled;
    const int row = index.row();
    if (row >= 0 && row <  m_items.size())
        if (m_items.at(row).editable) {
            rc |= Qt::ItemIsSelectable;
            // Can change name in list mode only
            if (m_viewMode == QListView::ListMode)
                rc |= Qt::ItemIsEditable;
        }
    return rc;
}

int WidgetBoxCategoryModel::rowCount(const QModelIndex & /*parent*/) const
{
    return m_items.size();
}

Widget WidgetBoxCategoryModel::widgetAt(const QModelIndex & index) const
{
    return widgetAt(index.row());
}

Widget WidgetBoxCategoryModel::widgetAt(int row) const
{
    if (row < 0 || row >=  m_items.size())
        return Widget();
    return m_items.at(row).widget;
}
